package com.xplusplus.security.controller;

import com.xplusplus.security.domain.Result;
import com.xplusplus.security.domain.Permission;
import com.xplusplus.security.domain.Role;
import com.xplusplus.security.service.PermissionService;
import com.xplusplus.security.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Huxudong
 * @Description: 角色Controller
 * @Date: Created in 12:45 2018/5/22
 * @Modified By:
 */
@RestController
@RequestMapping(value = "/permission")
@Api(tags = "权限接口")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 新增
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增")
    public Result<Permission> add(@Valid Permission permission, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultUtil.success(permissionService.save(permission));
    }

    /**
     * 更新
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新")
    public Result<Permission> update(@Valid Permission permission, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultUtil.success(permissionService.update(permission));
    }

    /**
     * 删除角色
     */
    @PostMapping(value = "/deleteById")
    @ApiOperation(value = "通过主键删除")
    public Result<Object> deleteById(@ApiParam(value = "主键") @RequestParam Integer id) {
        permissionService.delete(id);
        return ResultUtil.success();
    }

    @PostMapping(value = "/deleteByIdBatch")
    @ApiOperation(value = "批量删除")
    public Result<Object> deleteByIdBatch(@ApiParam(value = "主键数组") @RequestParam Integer[] ids) {
        permissionService.deleteInBatch(ids);
        return ResultUtil.success();
    }

    /**
     * 通过id查询
     */
    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过主键查询")
    public Result<Permission> getById(@ApiParam(value = "主键") @RequestParam Integer id) {
        return ResultUtil.success(permissionService.findOne(id));
    }

    /**
     * 查询所有
     */
    @GetMapping(value = "/getAll")
    @ApiOperation(value = "查询所有")
    public Result<List<Permission>> getAll() {
        return ResultUtil.success(permissionService.findAll());

    }

    /**
     * 查询所有-分页
     */
    @GetMapping(value = "/getAllByPage")
    @ApiOperation(value = "查询所有-分页")
    public Result<Page<Permission>> getAllByPage(
            @ApiParam(value = "页码", defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") Integer page,
            @ApiParam(value = "每页记录数", defaultValue = "10") @RequestParam(value = "size", defaultValue = "10") Integer size,
            @ApiParam(value = "排序字段名", defaultValue = "startTime") @RequestParam(value = "sortFieldName", defaultValue = "startTime") String sortFieldName,
            @ApiParam(value = "排序方向, 0降序; 1升序", defaultValue = "0") @RequestParam(value = "asc", defaultValue = "0") Integer asc) {
        return ResultUtil.success(permissionService.findAllByPage(page, size, sortFieldName, asc));
    }

    /**
     * 通过名称模糊查询-分页
     */
    @GetMapping(value = "/getByNameLikeByPage")
    @ApiOperation(value = "通过名称模糊查询-分页")
    public Result<Page<Permission>> getByNameLikeByPage(@ApiParam(value = "名称", defaultValue = "") @RequestParam(value = "name", defaultValue = "") String name,
                                                        @ApiParam(value = "页码", defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                        @ApiParam(value = "每页记录数", defaultValue = "10") @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                        @ApiParam(value = "排序字段名", defaultValue = "startTime") @RequestParam(value = "sortFieldName", defaultValue = "startTime") String sortFieldName,
                                                        @ApiParam(value = "排序方向, 0降序; 1升序", defaultValue = "0") @RequestParam(value = "asc", defaultValue = "0") Integer asc) {

        return ResultUtil.success(permissionService.findByNameLikeByPage(name, page, size, sortFieldName, asc));
    }

    /**
     * 通过名称模糊查询-分页
     */
    @GetMapping(value = "/getByRolesByPermissionId")
    @ApiOperation(value = "通过权限查询角色")
    public Result<List<Role>> getByRolesByPermissionId(@ApiParam(value = "权限id") @RequestParam Integer permissionId) {
        return ResultUtil.success(permissionService.getByRolesByPermissionId(permissionId));
    }

    @PostMapping(value = "/assignRolesToPermission")
    @ApiOperation(value = "把角色批量分配给权限")
    public Result<Object> assignRolesToPermission(@ApiParam(value = "权限id") @RequestParam Integer permissionId,
                                                  @ApiParam(value = "角色ids") @RequestParam Integer[] roleIds){
        permissionService.assignRolesToPermission(permissionId, roleIds);
        return ResultUtil.success();
    }
}
